package com.wanghan.salary.utils;

import com.wanghan.salary.config.EmailConfig;
import com.wanghan.salary.entity.MailObject;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

/**
 * @author wanghan
 */
public class EmailSend {


  /*  public static void main(String[] args) throws MessagingException {
        EmailConfig cofnig = new EmailConfig();
        EmailSend.sendEmail(cofnig,new String[]{"309342006@qq.com"}, "薪资", "王晗：当月工资100w");
    }*/

    public static void sendEmail(EmailConfig cofnig, String[] toes, String subject, String body) {
        MailObject mailObj = new MailObject();
        mailObj.setFrom(cofnig.getFrom());
        mailObj.setTo(toes);
        mailObj.setSubject(subject);
        mailObj.setFormat(MailObject.EmailFormat.HTML);
        mailObj.setBody(body);
        // 可以用msg.setRecipients方法增加多个接收人，指定接收人类型
        // Message.RecipientType.CC 抄送
        // Message.RecipientType.BCC 密送
        try {

            Authenticator authenticator = new MyAuthenticator(cofnig.getUser(), cofnig.getPass());
            // 创建会话
            Session session = Session.getInstance(cofnig, authenticator);
            // 构建邮件消息
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(mailObj.getFrom()));

            InternetAddress[] address = new InternetAddress[mailObj.getTo().length];
            for (int i = 0, j = mailObj.getTo().length; i < j; i++) {
                address[i] = new InternetAddress(mailObj.getTo()[i]);
            }
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(mailObj.getSubject());
            if (MailObject.EmailFormat.HTML.equals(mailObj.getFormat())) {
                // html格式
                msg.setContent(mailObj.getBody(), "text/html;charset=utf-8");
            } else {
                msg.setText(mailObj.getBody());
            }
            msg.setSentDate(new Date());
            // 发送邮件
            Transport.send(msg);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        System.out.println("邮件发送完成");
        System.out.println();
    }
/*
    public static void main(String[] args) throws MessagingException {
        MailObject mailObj = new MailObject();
        mailObj.setFrom("309342006@qq.com");
        mailObj.setTo(new String[]{"309342006@qq.com"});
        mailObj.setSubject("薪资");
        mailObj.setFormat(MailObject.EmailFormat.TEXT);
        mailObj.setBody("Java 收发邮件 (Jakarta Mail)");

        // smtp配置，可保存到properties文件，读取
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.qq.com");
        props.put("mail.smtp.port", 465);
        props.put("mail.smtp.ssl", true);
        // 需要认证
        props.put("mail.smtp.auth", true);
        props.put("mail.smtp.user", "309342006@qq.com");
        props.put("mail.smtp.pass", "clhxovljhhgobigd");
        // 使用ssl
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        Authenticator authenticator = new MyAuthenticator(props.getProperty("mail.smtp.user"), props.getProperty("mail.smtp.pass"));
        // 创建会话
        Session session = Session.getInstance(props, authenticator);
        // 构建邮件消息
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(mailObj.getFrom()));

        InternetAddress[] address = new InternetAddress[mailObj.getTo().length];
        for (int i = 0, j = mailObj.getTo().length; i < j; i++) {
            address[i] = new InternetAddress(mailObj.getTo()[i]);
        }
        // 可以用msg.setRecipients方法增加多个接收人，指定接收人类型
        // Message.RecipientType.CC 抄送
        // Message.RecipientType.BCC 密送
        msg.setRecipients(Message.RecipientType.TO, address);

        msg.setSubject(mailObj.getSubject());
        if (MailObject.EmailFormat.HTML.equals(mailObj.getFormat())) {
            // html格式
            msg.setContent(mailObj.getBody(), "text/html;charset=utf-8");
        } else {
            msg.setText(mailObj.getBody());
        }
        msg.setSentDate(new Date());

        // 发送邮件
        Transport.send(msg);
    }*/
}

class MyAuthenticator extends Authenticator {
    private String username;
    private String passwd;

    public MyAuthenticator(String username, String passwd) {
        this.username = username;
        this.passwd = passwd;
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        PasswordAuthentication auth = new PasswordAuthentication(username, passwd);
        return auth;
    }
}
